package com.example.zyfypt_713zxy.model;

import com.example.zyfypt_713zxy.bean.ArticleBean;
import com.example.zyfypt_713zxy.common.Common;
import com.example.zyfypt_713zxy.iface.ArticleListener;
import com.example.zyfypt_713zxy.iface.Articleiface;
import com.example.zyfypt_713zxy.service.ArticleService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ArticleModel implements Articleiface {

    private Retrofit retrofit;

    public ArticleModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList( String sessionID,String mod, int page, final ArticleListener listener) {
        ArticleService service = retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call = service.getArticleList(sessionID,mod,page);

        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body());
                else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFail(t.toString());
                System.out.println("--ArticleModel--"+t.toString());
            }
        });
    }
}
